package com.ciquan.server.controller;

import com.ciquan.server.bean.ArtistInfo;
import com.ciquan.server.bean.ArtistRecommend;
import com.ciquan.server.service.ArtistInfoService;
import com.ciquan.server.service.ArtistRecommendService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by kakalee on 15/3/27.
 */
@Controller
@RequestMapping("/artistRecommend")
public class ArtistRecommendController extends AbstractController {

    @Resource
    private ArtistRecommendService artistRecommendService;

    @Resource
    private ArtistInfoService artistInfoService;

    @RequestMapping("/list")
    public ModelAndView list(ModelMap modelMap) {
        List<ArtistRecommend> artistRecommends = artistRecommendService.selectList();
        modelMap.put("artistRecommends", artistRecommends);
        return new ModelAndView("artistRecommendList", modelMap);
    }

    @RequestMapping("add")
    public ModelAndView add(ModelMap modelMap) {
        List<ArtistInfo> artistInfos = artistInfoService.selectList();
        modelMap.put("artistInfos", artistInfos);
        return new ModelAndView("addArtistRecommend", modelMap);
    }

    @RequestMapping("saveAdd")
    public ModelAndView saveAdd(HttpServletRequest request) {
        int artistId = ServletRequestUtils.getIntParameter(request, "artistId", 0);
        ArtistRecommend artistRecommend = new ArtistRecommend();
        artistRecommend.setArtistId(artistId);
        artistRecommendService.insert(artistRecommend);
        return new ModelAndView("redirect:/artistRecommend/list");
    }

    @RequestMapping("edit")
    public ModelAndView edit(HttpServletRequest request, ModelMap modelMap) {
        int id = ServletRequestUtils.getIntParameter(request, "id", 0);
        int artistId = ServletRequestUtils.getIntParameter(request, "artistId", 0);
        ArtistRecommend artistRecommend = new ArtistRecommend();
        artistRecommend.setId(id);
        artistRecommend.setArtistId(artistId);
        List<ArtistInfo> artistInfos = artistInfoService.selectList();

        modelMap.put("artistRecommend", artistRecommend);
        modelMap.put("artistInfos", artistInfos);
        return new ModelAndView("editArtistRecommend", modelMap);
    }

    @RequestMapping("saveEdit")
    public ModelAndView saveEdit(HttpServletRequest request) {
        int id = ServletRequestUtils.getIntParameter(request, "id", 0);
        int artistId = ServletRequestUtils.getIntParameter(request, "artistId", 0);
        ArtistRecommend artistRecommend = new ArtistRecommend();
        artistRecommend.setId(id);
        artistRecommend.setArtistId(artistId);
        artistRecommendService.update(artistRecommend);
        return new ModelAndView("redirect:/artistRecommend/list");
    }
}
